<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/10
 * Time: 17:39
 */
namespace common\components\services\message;

use common\components\bases\BaseBehavior;
use common\components\helpers\EventData;

class MessageBehavior extends BaseBehavior{

    public function afterInsert($event){
        $eventKey=(new EventData(['className'=>get_class($this),'classOwner'=>get_class($this->owner),'eventName'=>$event->name,'handler'=>__FUNCTION__]))->getEventKey();
        $messageManage=\Yii::createObject([
            'class'=>MessageEventManage::className(),
            'eventArray'=>$event,
            'ownerArray'=>$this->owner->toArray(),
            'eventKey'=>$eventKey
        ]);
        $messageManage->afterInsertSend();
    }
    public function sendTelephoneValidateCode($event){
        $eventKey=(new EventData(['className'=>get_class($this),'classOwner'=>get_class($this->owner),'eventName'=>$event->name,'handler'=>__FUNCTION__]))->getEventKey();
        $messageManage=\Yii::createObject([
            'class'=>MessageEventManage::className(),
            'eventArray'=>$event,
            'ownerArray'=>$event->action['data'],
            'eventKey'=>$eventKey
        ]);
        $messageManage->sendTelephoneValidateCode();
    }

    public function sendSmsMessage($event){
        $eventKey=(new EventData(['className'=>get_class($this),'classOwner'=>get_class($this->owner),'eventName'=>$event->name,'handler'=>__FUNCTION__]))->getEventKey();
        $messageManage=\Yii::createObject([
            'class'=>MessageEventManage::className(),
            'eventArray'=>$event,
            'ownerArray'=>$event->action['data'],
            'eventKey'=>$eventKey
        ]);
        $messageManage->sendTelephoneValidateCode();
    }

    /*针对订单状态发送的信息*/
    public function checkoutOrderforSendMsg($event){
        $eventKey=(new EventData(['className'=>get_class($this),'classOwner'=>get_class($this->owner),'eventName'=>$event->name,'handler'=>__FUNCTION__]))->getEventKey();
        $order = $this->owner;
        $changeattr = $event->changedAttributes;
        $attr = $this->owner->getAttributes();
        if(isset($changeattr['pay_status']) && $changeattr['pay_status'] != $attr['pay_status']){//仅当pay_status状态更改的时候发送消息
            $paystatus_changed = true;
        }else{
            $paystatus_changed = false;
        }
        if(isset($order->pay_status) && $order->pay_status != 0 && $paystatus_changed){//预约时支付状态为1的时候触发；订单状态更改时候触发
            $messageManage=\Yii::createObject([
                'class'=>MessageEventManage::className(),
                'eventArray'=>$event,
                'ownerArray'=>$this->owner->toArray(),
                'eventKey'=>$eventKey
            ]);
            $messageManage->afterInsertSend();
        }

    }

    /*处理结果发站内消息*/
    public function handlerSend($event){
        $eventKey=(new EventData(['className'=>get_class($this),'classOwner'=>get_class($this->owner),'eventName'=>$event->name,'handler'=>__FUNCTION__]))->getEventKey();
        $model = $this->owner;
        if($model->status > 1){
            $messageManage=\Yii::createObject([
                'class'=>MessageEventManage::className(),
                'eventArray'=>$event,
                'ownerArray'=>$this->owner->toArray(),
                'eventKey'=>$eventKey
            ]);
            $messageManage->afterInsertSend();
        }
    }
    /*上课消息*/
    public function classSend($event){
        $eventKey=(new EventData(['className'=>get_class($this),'classOwner'=>get_class($this->owner),'eventName'=>$event->name,'handler'=>__FUNCTION__]))->getEventKey();
        $messageManage=\Yii::createObject([
            'class'=>MessageEventManage::className(),
            'eventArray'=>$event,
            'ownerArray'=>$event->action['data'],
            'eventKey'=>$eventKey
        ]);

       // print_r($messageManage);die;
        $messageManage->afterInsertSend();

    }
}